<template>
	<div class="login">
		<div class="login_box">
			<div class="login-title">登录</div>
			<form @submit.prevent="login">
				<div class="phone">
					<!-- <span>手机号</span> -->
					<input type="text" placeholder="请输入手机号" v-model="phone">
					<i class="iconfont icon-mobile"></i>
				</div>
				<div class="password">
					<!-- <span>手机号</span> -->
					<input type="password" placeholder="请输入密码" v-model="password">
					<i class="iconfont icon-mima"></i>
				</div>
				<div class="login-btn">
					<button type="submit">确定</button>
				</div>
				<div class="resetPW">
					<span>忘记密码？</span>
					<a href="javascript:;" @click="$router.push('resetPassword')">点击找回</a>
				</div>
			</form>
		</div>
		<div class="com-info">
            <p><a target="_blank" href="http://www.beian.miit.gov.cn">蜀ICP备17028712号-1</a> 火星云网络科技有限公司 huoxingy.com</p>
        </div>
	</div>
</template>

<script type="text/javascript">
import {API_BASE_URL} from "./../../urlConfig/index";
export default {
	data(){
		return{
			phone: '', // 手机号
			password: '' ,// 密码
		}
	},
	methods: {
		login: function () { // 登录事件
		    if ( !(/^1[3|4|5|7|8][0-9]{9}$/.test(this.phone)) ) {
		        this.$message.error({
		            message: "请输入正确的手机号",
		            type: 'error',
		        });
		        return;
		    }
		    if ( !this.password ) {
		        this.$message.error({
		            message: "请输入密码",
		            type: 'error'
		        });
		        return;
		    }

		    let data = {
		        "username": this.phone,
		        "password": this.password,
		    }
		    let b = new Base64();
		    let authStr = 'Basic '+ b.encode(data.username+':'+data.password);

		    this.$http.post(API_BASE_URL+'api/v1/sales/login', {}, {
		    // this.$http.post("https://staging-api.huoxingy.com/v1/admin/login", {}, {
		    	headers: {'X-Requested-With': 'XMLHttpRequest'},
		        auth: data // 添加登录验证
		    }).then((res)=>{
		    	localStorage.setItem('token', authStr); // 缓存token验证
		    	this.$http.get("profile").then((res)=>{
					localStorage.setItem('userType', res.data.userType);//缓存邀请码等级
					if(res.data.mobile=="18482220260"){
						sessionStorage.setItem("userData",JSON.stringify(res.data));
					}
		    	    if ( res.data.idCardNo ) {
						this.$router.push({path:'/store'});	
						
		    	    } else {
				    	this.$router.push({path:'/fillInfo'});	
		    	    }
		    	}).catch((err)=>{

		    	})
		    }).catch((err)=>{
		    	console.log(err);
		    	if ( err.response ) {
		    	    if ( err.response.status == 401 ) {
		    	        this.$message({
		    	            message: "帐号或者密码有误",
		    	            type: 'error'
		    	        })
		    	    } else if ( err.response.status == 403 ) {
		    	        this.$message({
		    	            message: "权限不足",
		    	            type: 'error'
		    	        })
		    	    } else {
		    	        this.$message({
		    	            message: err.response.data,
		    	            type: 'error'
		    	        })
		    	    }
		    	}
		    })
		},
	},
	created: function () {
            // if ( localStorage.getItem('token') ) {
            //     this.$router.push('store');
            // }
        },
	mounted(){
  	}
}
</script>
<style lang="scss" scoped>
	@import "login";
</style>
